package com.live.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.live.model.Contract;
import com.live.util.DBUtil;
import com.live.util.RowMapper;

public class ContractDao {
	public int addContract(Contract contract){
		String sql = "insert into contract(firstPartyName ,contacts, partyAddres, telephone , adminId,startTime,endTime,contractNo,contractAmount,unit,whetherPay,payTime,remarks) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] param = {contract.getFirstPartyName(),contract.getContacts(),contract.getPartyAddres(),contract.getTelephone(),contract.getAdminId(),contract.getStartTime(),contract.getEndTime(),contract.getContractNo(),contract.getContractAmount(),contract.getUnit(),contract.getWhetherPay(),contract.getPayTime(),contract.getRemarks()};
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public int editContract(Contract contract) {
		String sql = "update contract set firstPartyName=? ,contacts=?,partyAddres=?, telephone =?, adminId=?,startTime=?,endTime=?,contractNo=?,contractAmount=?,unit=?,whetherPay=?,payTime=?,remarks=? where id=?";
		Object[] param = { contract.getFirstPartyName(),contract.getContacts(),contract.getPartyAddres(),contract.getTelephone(),contract.getAdminId(),contract.getStartTime(),contract.getEndTime(),contract.getContractNo(),contract.getContractAmount(),contract.getUnit(),contract.getWhetherPay(),contract.getPayTime(),contract.getRemarks(),contract.getId() };
		try {
			DBUtil.executeUpdate(sql, param);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public List<Contract> getPageContract(String firstPartyName,int pageIndex, int pageSize) {
		String sql = "select c.*,d.name as  adminName from contract c left join admin d on c.adminId=d.id   where c.firstPartyName like ? order by c.startTime asc limit ?,?";
		Object[] Param = { "%" + firstPartyName + "%", (pageIndex - 1) * pageSize, pageSize };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				List<Contract> data = new ArrayList<Contract>();
				while (rs.next()) {
					Contract contract = new Contract();
					contract.setId(rs.getInt("id"));
					contract.setFirstPartyName(rs.getString("firstPartyName"));
					contract.setPayTime(rs.getString("payTime"));
					contract.setContacts(rs.getString("contacts"));
					contract.setTelephone(rs.getString("telephone"));
					contract.setPartyAddres(rs.getString("partyAddres"));
					contract.setStartTime(rs.getString("startTime"));
					contract.setEndTime(rs.getString("endTime"));
					contract.setUnit(rs.getString("unit"));
					contract.setContractNo(rs.getString("contractNo"));
					contract.setContractAmount(rs.getInt("contractAmount"));
					contract.setWhetherPay(rs.getInt("whetherPay"));
					contract.setRemarks(rs.getString("remarks"));
					contract.setAdminId(rs.getInt("adminId"));
					contract.setAdminName(rs.getString("adminName"));
					data.add(contract);
				}
				return data;
			}
		}, Param);
		List<Contract> list = (List<Contract>) result;
		return list;
	}
	public int getTotalCount(String firstPartyName) {
		String sql = "select count(*)totalcount from contract where  firstPartyName like ?";
		Object[] param = {  "%" + firstPartyName + "%" };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				int totalCount = 0;
				while (rs.next()) {
					totalCount = rs.getInt("totalCount");
				}
				return totalCount;
			}
		},param);
		return Integer.valueOf(String.valueOf(result));
	}
	public Contract getContract(int id) {
		Contract contract = null;
		String sql = "select  * from contract where id=? ";
		Object[] param = { id };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				Contract data = null;
				while (rs.next()) {
					data = new Contract();
					data.setId(rs.getInt("id"));
					data.setFirstPartyName(rs.getString("firstPartyName"));
					data.setPayTime(rs.getString("payTime"));
					data.setContacts(rs.getString("contacts"));
					data.setTelephone(rs.getString("telephone"));
					data.setPartyAddres(rs.getString("partyAddres"));
					data.setStartTime(rs.getString("startTime"));
					data.setEndTime(rs.getString("endTime"));
					data.setUnit(rs.getString("unit"));
					data.setContractNo(rs.getString("contractNo"));
					data.setContractAmount(rs.getInt("contractAmount"));
					data.setWhetherPay(rs.getInt("whetherPay"));
					data.setRemarks(rs.getString("remarks"));
					data.setAdminId(rs.getInt("adminId"));
				}
				return data;
			}
		}, param);
		if (result != null) {
			contract	 = (Contract) result;
		}
		return contract;
	}
	public Contract getContractNo(String contractNo) {
		Contract contract = null;
		String sql = "select * from contract where contractNo=?  ";
		Object[] param = { contractNo };
		Object result = DBUtil.executeQuery(sql, new RowMapper() {
			@Override
			public Object mapper(ResultSet rs) throws SQLException {
				Contract data = null;
				while (rs.next()) {
					data = new Contract();
					data.setId(rs.getInt("id"));
					data.setFirstPartyName(rs.getString("firstPartyName"));
					data.setPayTime(rs.getString("payTime"));
					data.setContacts(rs.getString("contacts"));
					data.setTelephone(rs.getString("telephone"));
					data.setPartyAddres(rs.getString("partyAddres"));
					data.setStartTime(rs.getString("startTime"));
					data.setEndTime(rs.getString("endTime"));
					data.setUnit(rs.getString("unit"));
					data.setContractNo(rs.getString("contractNo"));
					data.setContractAmount(rs.getInt("contractAmount"));
					data.setWhetherPay(rs.getInt("whetherPay"));
					data.setRemarks(rs.getString("remarks"));
					data.setAdminId(rs.getInt("adminId"));
				}
				return data;
			}
		}, param);
		if (result != null) {
			contract = (Contract) result;
		}
		return contract;
	}
}
